Source code for /engineering/webperf/slave-v4[j1.2]/Scripter.javaOriginal file Scripter.java
   1 // Script handler -- I wanna set this up the script ahead of time, cause 
   2 // it is easier.  :D
   3 
   4 import java.util.*;
   5 import java.io.*;
   6 
   7 public class Scripter {
   8 
   9    long		start; 
  10 	
  11    private Vector	chute;
  12 
  13    public int     userList[];
  14 
  15    public int	numberItems;
  16    public String	name;
  17    public int	users;
  18    public int 	chutes;
  19    public int	errors;
  20 
  21    Scripter () {
  22 
  23       chute 	= new Vector(10000, 1000);   // Wild-assed capacity guesses
  24 	numberItems = 0;
  25 	name 		= null;
  26    }
  27 
  28    public String load(String  text) {
  29 
  30 	ScriptItem    item;
  31 	String	  param;
  32 
  33 	int		  index  		= 0;
  34 	int 		  textLineNum	= 1;	
  35 	boolean	  error;
  36 
  37 	boolean	  gotName		= false;
  38 	boolean	  gotChute		= false;
  39 	boolean	  fatalError	= false;
  40 
  41 	Integer	  i;
  42 
  43 	int		  tempParam;
  44 
  45 	// For the tokenizer
  46 	StringBuffer  a;
  47 	StringBuffer  b;
  48 	StringBuffer  c;
  49 	char		  temp;
  50 	int		  size;
  51 	int		  rover;   
  52 	int		  tc;
  53 
  54 	StringBuffer report = new StringBuffer();
  55 
  56 	// read as a stream until empty
  57 	StringReader   s 	    = new StringReader(text);
  58 	BufferedReader source = new BufferedReader(s); 
  59 	String	   dest   = new String();
  60 
  61 	Hashtable	   jumpList = new Hashtable();
  62 
  63 	errors	   = 0;
  64 	
  65 	report.append("LOADING.\n");
  66 	try {
  67 	   dest = source.readLine();
  68 	   while ((dest != null)&&(fatalError == false)) {	// will it null for an empty line? hmmm
  69 		error = false;
  70 		if ((dest.length() > 1)&&(dest.charAt(0)!= ScriptItem.commentChar)) {
  71 			item    = new ScriptItem();
  72 
  73 			// Convert the line into string tokens.
  74 			size     = dest.length();
  75 			rover    = 0;
  76 			tc	   = 0;
  77 			a	   = new StringBuffer();
  78 			b	   = new StringBuffer();
  79 			c	   = new StringBuffer(); 
  80 			while (rover < size) {
  81         		   temp = dest.charAt(rover);
  82 			   if (temp == (char)ScriptItem.tokenDelimit) tc++;
  83 	   		   else {
  84             		switch (tc) {
  85 					case 0: a.append(temp); break;
  86 					case 1: b.append(temp); break;
  87 					case 2: c.append(temp); break;
  88 					default:  //for now, we will let extra delimiters be ignored.
  89 			      }
  90 	   		   }		
  91 	   		   rover++;
  92 			}
  93 
  94 			item.token = toToken(a.toString());
  95 
  96 			try {
  97 				item.text = c.toString();
  98 			} catch (Exception e) {
  99 			}	
 100 
 101 			// special processing for each token
 102 			switch (item.token) {
 103 			   case ScriptItem.tokenNone:
 104 				 error = true;
 105 				 errors++;
 106 				 report.append("Could not parse line #" + textLineNum + "\n");
 107 				 report.append("  : " + dest + "\n");
 108 				 break;
 109 
 110 			   case ScriptItem.tokenName:
 111 				 if (gotName == true) {
 112 				    report.append("FATAL ERROR:  Only on NAME allowed per script.\n");
 113 				    fatalError = true;
 114 				    break;	
 115 				 }
 116 				 name  = item.text;
 117 				 try {
 118 				    users = Integer.parseInt(b.toString());
 119 				 } catch (Exception e) {
 120 				    report.append("Could not parse USER # in line #" + textLineNum + "\n");
 121 				    error = true;
 122 				    errors++;
 123 				    break;	
 124 				 }
 125 				 userList = new int[users+1];
 126 				 for(rover=0; rover <= users; rover++) {userList[rover]=0;};
 127 				 gotName = true;	 
 128 				 break;
 129 
 130 			   case ScriptItem.tokenChute:
 131 				 if (gotChute == true) {
 132 				    report.append("FATAL ERROR:  Only on CHUTE allowed per script.\n");
 133 				    fatalError = true;
 134 				    break;	
 135 				 }
 136 				 if (gotName == false) {
 137 				    report.append("FATAL ERROR:  Must specify NAME before CHUTE.\n");
 138 				    fatalError = true;
 139 				    break;	
 140 				 }
 141 				 try {
 142 				    chutes = Integer.parseInt(b.toString());
 143 				 } catch (Exception e) {
 144 				    report.append("Could not parse CHUTE # in line #" + textLineNum + "\n");
 145 				    error = true;
 146 				    errors++;
 147 				    break;	
 148 				 }
 149 				 gotChute = true;	 
 150 				 break;
 151 	
 152 			   case ScriptItem.tokenPut:
 153 				 try {
 154 				    item.id = Integer.parseInt(b.toString());
 155 				 } catch (Exception e) {
 156 				    item.id = 0;
 157 				 }
 158 				 chute.addElement(item);
 159 			       numberItems++; 
 160 				 break;
 161 
 162 			   case ScriptItem.tokenIndexPut:
 163 				 chute.addElement(item);
 164 			       numberItems++; 
 165 				 break;
 166 
 167 			   case ScriptItem.tokenWait:
 168 				 try {
 169 				    item.count = Integer.parseInt(b.toString());
 170 				 } catch (NumberFormatException eN) {
 171 				    report.append("Could not parse WAIT value in line #" + textLineNum + "\n");
 172 				    error = true;
 173 				    errors++;
 174 				    break;	
 175 				 }
 176 				 try {
 177 				    item.id = Integer.parseInt(item.text);
 178 				 } catch (NumberFormatException eN) {
 179 				    item.id = 0;
 180 				 }	
 181 			   	 chute.addElement(item);
 182 			       numberItems++; 
 183 				 break;
 184 
 185 			   case ScriptItem.tokenLoop:
 186 				 try {
 187 				    item.count = Integer.parseInt(b.toString());
 188 			   	    chute.addElement(item);
 189 				    jumpList.put(item.text, new Integer(numberItems));
 190 			          numberItems++; 
 191 				 } catch (NumberFormatException eN) {
 192 				    report.append("Could not parse LOOP COUNT value in line #" + textLineNum + "\n");
 193 				    error = true;
 194 				    errors++;
 195 				    break;
 196 				 }
 197 				 break;
 198 
 199 			   case ScriptItem.tokenEndLoop:
 200 				 try {
 201 				    i=(Integer)jumpList.get(item.text);
 202 				    if (i == null) {
 203 				       report.append("Unknown loop symbol in line #" + textLineNum + "\n");
 204 				       error = true;
 205 				       errors++;
 206 				    } else {
 207 				       item.count   = i.intValue();	
 208 			   	       chute.addElement(item);
 209 			             numberItems++; 
 210 				    }
 211 				 } catch (Exception em) {
 212 					report.append("General symbol error in line #" + textLineNum + "\n");
 213 				      error = true;
 214 				      errors++;
 215 				 }
 216 				 break;
 217 
 218 			   case ScriptItem.tokenSync:
 219 				 try {
 220 				    item.count = Integer.parseInt(b.toString());
 221 			   	    chute.addElement(item);
 222 			          numberItems++; 
 223 				 } catch (NumberFormatException eN) {
 224 				    report.append("Could not parse SYNC value in line #" + textLineNum + "\n");
 225 				    error = true;
 226 				    errors++;
 227 				    break;
 228 				 }
 229 				 try {
 230 				    item.id = Integer.parseInt(item.text);
 231 				 } catch (NumberFormatException eN) {
 232 				    item.id = 0;
 233 				 }	
 234 				 break;
 235 
 236 			   case ScriptItem.tokenIndexSet:
 237 				 try {
 238 				    item.count = Integer.parseInt(b.toString());
 239 				 } catch (Exception e) {
 240 				    report.append("Could not parse INDEX SET value in line #" + textLineNum + "\n");
 241 				    error = true;
 242 				    errors++;
 243 				    break;	
 244 				 }
 245 				 chute.addElement(item);
 246 			       numberItems++; 
 247 				 break;
 248 
 249 
 250 			   case ScriptItem.tokenIndexInc:
 251 			   case ScriptItem.tokenEnd:
 252 				 // Dont have to do anything
 253 			   	 chute.addElement(item);
 254 			       numberItems++; 
 255 				 break;
 256 
 257 			   case ScriptItem.tokenIndexWait:
 258 				 try {
 259 				    item.count = Integer.parseInt(b.toString());
 260 				 } catch (NumberFormatException eN) {
 261 				    report.append("Could not parse INDEX WAIT value in line #" + textLineNum + "\n");
 262 				    error = true;
 263 				    errors++;
 264 				    break;	
 265 				 }
 266 			   	 chute.addElement(item);
 267 			       numberItems++; 
 268 				 break;
 269 
 270 			   case ScriptItem.tokenAssign:
 271 				 try {
 272 				   size  = Integer.parseInt(b.toString());
 273 				   rover = Integer.parseInt(c.toString());
 274 				   if (rover > chutes) {
 275 				      report.append("Not a valid chute in line #" + textLineNum + "\n");
 276 				      error = true;
 277 				      errors++;
 278 				      break;	
 279 				   }
 280  				   userList[size] = rover;
 281 				 } catch (Exception e) {
 282 				    report.append("Assign out of order or out of bounds in line #" + textLineNum + "\n");
 283 				    error = true;
 284 				    errors++;
 285 				    break;	
 286 				 }
 287 				 break;
 288 	
 289 			   case ScriptItem.tokenBAD:
 290 
 291  			   default:
 292 				 error = true;
 293 				 errors++;
 294  				 report.append("Software fault at line #" + textLineNum + "\n");
 295 			} 
 296 		}
 297 		textLineNum++;			
 298 	      dest = source.readLine();
 299 	   }			
 300 
 301 	} catch (IOException e) {
 302 
 303       } finally {
 304 	   try {
 305 		source.close();
 306 	   	s.close();
 307 	  } catch (IOException e) {
 308 		// blah blah
 309 	  }
 310 	}
 311 	
 312 	if ((gotChute==false)||(gotName==false)) {
 313 	   errors++;
 314 	   report.append("Script must set both Name and # of Chutes\n\n");
 315 	}
 316 
 317 	report.append("LOADED : " + errors + " errors found.\n\n");
 318 	return report.toString();
 319    }
 320 
 321    public  String getName() {
 322 	return name;
 323    }
 324 
 325    public  ScriptItem getItem(int  index) {
 326 	return (ScriptItem) chute.elementAt(index);
 327    } 
 328    
 329    private int toToken(String text) {
 330    	if (text.equals(ScriptItem.tokenTextName)) 	   return ScriptItem.tokenName;
 331 	else if (text.equals(ScriptItem.tokenTextPut))     return ScriptItem.tokenPut;
 332 	else if (text.equals(ScriptItem.tokenTextWait))    return ScriptItem.tokenWait;
 333 	else if (text.equals(ScriptItem.tokenTextEndLoop)) return ScriptItem.tokenEndLoop;
 334 	else if (text.equals(ScriptItem.tokenTextLoop))    return ScriptItem.tokenLoop;
 335 	else if (text.equals(ScriptItem.tokenTextEnd))     return ScriptItem.tokenEnd;
 336 	else if (text.equals(ScriptItem.tokenTextSync))    return ScriptItem.tokenSync;
 337 	else if (text.equals(ScriptItem.tokenTextIndexPut)) return ScriptItem.tokenIndexPut;
 338 	else if (text.equals(ScriptItem.tokenTextIndexSet)) return ScriptItem.tokenIndexSet;
 339 	else if (text.equals(ScriptItem.tokenTextIndexInc)) return ScriptItem.tokenIndexInc;
 340 	else if (text.equals(ScriptItem.tokenTextChute))    return ScriptItem.tokenChute;
 341 	else if (text.equals(ScriptItem.tokenTextAssign))   return ScriptItem.tokenAssign;
 342 	else if (text.equals(ScriptItem.tokenTextIndexWait))   return ScriptItem.tokenIndexWait;
 343 	else return ScriptItem.tokenNone;
 344    }     
 345 
 346 }